<template>
    <div class="top-header">
        <div class="top-img"></div>
    </div>	
    <h1>{{ $t('Logging_in') }} ...</h1>
</template>

<script setup>
	import axios from '@/utils/api'
    import {ref } from 'vue'
	import {useRoute,useRouter} from 'vue-router'
    import { useUserInfoStore } from '@/store/modules/user_info/index';
    const user_infoStore = useUserInfoStore();
	const route = useRoute()
	const router = useRouter()

    const lang = ref('')
	if(localStorage.getItem('lang') == 'en'){
		lang.value = 'en'
	}else{
		lang.value = 'zh'
	}
	const fb_login =async ()=>{
		try {
            const state = route.query.state ? route.query.state : ''
            const res = await axios.get('/yh_login?code='+route.query.code+'&state='+state)
            console.log(res);
            if(res.code == 0){
                if(lang.value == 'en'){
                    ElMessage.success('Login successful')
                }else{
                    ElMessage.success('登錄成功')
                }
                
                user_infoStore.setUserToken(res.data.token)
                user_infoStore.setUserToken_expire(res.data.expire)
                user_infoStore.setUserinfo(res.data.user_info)
                
                
                if(window.innerWidth < 1024 && window.baseUrl == "http://192.168.110.244:8081/game/public"){
                    if(localStorage.getItem('otherLogin')){
                        const url = localStorage.getItem('otherLogin');
                        localStorage.removeItem('otherLogin');
                        window.location.href = url
                        return
                    }else{
                        window.location.href = 'http://localhost:5178/#/pages/My/index';
                        return
                    }
                    
                    console.log('跳转本地移动端链接');
                }
                if(window.innerWidth < 1024 && window.baseUrl !== "http://192.168.110.244:8081/game/public"){
                    if(localStorage.getItem('otherLogin')){
                        const url = localStorage.getItem('otherLogin');
                        localStorage.removeItem('otherLogin');
                        window.location.href = url
                        return
                    }else{
                        window.location.href = window.hostUrl+'/pages/My/index';
                        return
                    }
                    
                    console.log('跳转测试移动端');
                }
                if(localStorage.getItem('otherLogin')){
                    const url = localStorage.getItem('otherLogin');
                    localStorage.removeItem('otherLogin');
                    window.location.href = url
                }else{
                    window.location.href = '//'+window.location.hostname+'/#/home'
                }
            }else if(res.code == 1){
                if(lang.value == 'en'){
                    ElMessage.success('Registration and login successful')
                }else{
                    ElMessage.success('註冊登錄成功')
                }
            
                user_infoStore.setUserToken(res.data.token)
                user_infoStore.setUserToken_expire(res.data.expire)
                user_infoStore.setUserinfo(res.data.user_info)
                // router.push('/home')

                if(window.innerWidth < 1024 && window.baseUrl == "http://192.168.110.244:8081/game/public"){
                    if(localStorage.getItem('otherLogin')){
                        const url = localStorage.getItem('otherLogin');
                        localStorage.removeItem('otherLogin');
                        window.location.href = url
                        return
                    }else{
                        window.location.href = 'http://localhost:5178/#/pages/My/index';
                        return
                    }
                    
                    console.log('跳转本地移动端链接');
                }
                if(window.innerWidth < 1024 && window.baseUrl !== "http://192.168.110.244:8081/game/public"){
                    if(localStorage.getItem('otherLogin')){
                        const url = localStorage.getItem('otherLogin');
                        localStorage.removeItem('otherLogin');
                        window.location.href = url
                        return
                    }else{
                        window.location.href = window.hostUrl+'/pages/My/index';
                        return
                    }
                    console.log('跳转测试移动端');
                }
                if(localStorage.getItem('otherLogin')){
                    const url = localStorage.getItem('otherLogin');
                    localStorage.removeItem('otherLogin');
                    window.location.href = url
                }else{
                    window.location.href = '//'+window.location.hostname+'/#/home'
                }
            }else{
                ElMessage.error(res.message)
                // router.push('/login')

                if(window.innerWidth < 1024 && window.baseUrl == "http://192.168.110.244:8081/game/public"){
                    window.location.href = 'http://localhost:5178/#/pages/Login/index';
                    console.log('跳转本地移动端链接');
                }
                if(window.innerWidth < 1024 && window.baseUrl !== "http://192.168.110.244:8081/game/public"){
                    window.location.href = window.hostUrl+'/pages/Login/index';
                    console.log('跳转测试移动端');
                }
                window.location.href = '//'+window.location.hostname+'/#/login'
            }
        } catch (error) {
            if(window.innerWidth < 1024 && window.baseUrl == "http://192.168.110.244:8081/game/public"){
                window.location.href = 'http://localhost:5178/#/pages/Login/index';
                console.log('跳转本地移动端链接');
            }
            if(window.innerWidth < 1024 && window.baseUrl !== "http://192.168.110.244:8081/game/public"){
                window.location.href = window.hostUrl+'/pages/Login/index';
                console.log('跳转测试移动端');
            }
            window.location.href = '//'+window.location.hostname+'/#/login'
        }
	}
    fb_login()
</script>

<style lang="less" scoped>
.top-header {
    width: 100%;
    height: 110px;
    z-index: 10;
    background: linear-gradient(#2ca0d8, #2ca0d8 100%, #fff 0, #fff);
    .top-img {
        width: 100%;
        height: 106px;
        margin: 0 auto;
        background: url(https://yyfuns.com/static/logoZ.png) no-repeat;
		background-size: contain;
		background-position: center;
        transform: scale(.8);
        filter: invert(1) brightness(30);
        // background: url(https://s1.hdslb.com/bfs/static/jinkela/passport-pc/assets/rl_top.png) no-repeat;
    }
}
h1{
    text-align: center;
    margin-top: 100px;
}
</style>